/*
 * @Description:
 * @Author: your name
 * @version:
 * @Date: 2024-01-27 11:06:25
 * @LastEditors: your name
 * @LastEditTime: 2024-01-27 16:10:03
 */
//光柱椎体类
import * as Cesium from "cesium";
// 底部贴图圆
import ConeGlowBottomCircleMaterialProperty from "./textureCircle";
// 底部运动圆环
import ConeGlowBottomRotateCircleMaterialProperty from "./movingCircle";

export default class ConeGlow {
  constructor(viewer, position, options) {
    this.viewer = viewer;
    this.position = position;
    const {lng,lat,height}=this.position
    this.positionCar3=Cesium.Cartesian3.fromDegrees(
        lng,
        lat,
        height
      );
    this.options = options; //color 颜色 height 光柱高度 bottomRadius 光锥的底部半径
    this.height = options.height || 3000;
    this.bottomRadius = options.bottomRadius || 150;
    this.color = options.color || Cesium.Color.AQUA; //给个亮亮的颜色
  }

  //添加圆锥体
  addCone() {
    // 这里需要把椎体的位置，向上提升一半
    let modelMatrix = Cesium.Matrix4.multiplyByTranslation(
      Cesium.Transforms.eastNorthUpToFixedFrame(this.positionCar3),
      new Cesium.Cartesian3(0.0, 0.0, this.height * 0.5),
      new Cesium.Matrix4()
    );

    let cylinderGeometry = new Cesium.CylinderGeometry({
      length: this.height,
      topRadius: 0,
      bottomRadius: this.bottomRadius,
      vertexFormat:
        Cesium.MaterialAppearance.MaterialSupport.TEXTURED.vertexFormat,
    });
    let cone = new Cesium.GeometryInstance({
      geometry: cylinderGeometry,
      modelMatrix: modelMatrix,
    });
    this.cylinderPrimitive = this.viewer.scene.primitives.add(
      new Cesium.Primitive({
        geometryInstances: [cone],
        appearance: new Cesium.MaterialAppearance({
          material: new Cesium.Material({
            fabric: {
              type: "cone",
              uniforms: {
                color: this.color,
              },
              source: /*glsl*/ `
                            czm_material czm_getMaterial(czm_materialInput materialInput){
                                float ITime=fract(czm_frameNumber/10.);
                                czm_material material = czm_getDefaultMaterial(materialInput);
                                // 经过测试发现，使用vec3的str判断距离，效果并不好，改用vec2来判断，效果比较好
                                vec2 st=materialInput.st;
                                vec2 center=vec2(.5,.5);
                                float dis=distance(st,center);
                                dis=clamp(dis,.0,1.);
                                float isAlpha=step(.5,ITime);
                                material.alpha=dis;
                                float Intensity=2.+.5*isAlpha;
                                material.alpha*=Intensity;
                                material.diffuse=color.rgb;
                                return material;
                            }
                            `,
            },
            translucent: false,
          }),
          faceForward: false, // 当绘制的三角面片法向不能朝向视点时，自动翻转法向，从而避免法向计算后发黑等问题
          closed: true, // 是否为封闭体，实际上执行的是是否进行背面裁剪
        }),
      })
    );
    return this.cylinderPrimitive;
  }

  //添加底部圆
  addBottomCircle() {
    this.bottomCircle = this.viewer.entities.add({
      position: this.positionCar3,
      ellipse: {
        semiMinorAxis: this.bottomRadius * 2,
        semiMajorAxis: this.bottomRadius * 2,
        height: this.position.height,
        material: new ConeGlowBottomCircleMaterialProperty(this.color),
      },
    });
    return this.bottomCircle;
  }

  //添加底部旋转圆
  addBottomRotateCircle() {
    let stRotation = 360;
    this.bottomRotateCircle = this.viewer.entities.add({
      position: this.positionCar3,
      ellipse: {
        semiMinorAxis: this.bottomRadius * 1.45,
        semiMajorAxis: this.bottomRadius * 1.45,
        height: this.position.height,
        material: new ConeGlowBottomRotateCircleMaterialProperty(this.color),
        stRotation: new Cesium.CallbackProperty((e) => {
          stRotation--;
          stRotation < 0 && (stRotation = 360);
          return Cesium.Math.toRadians(stRotation);
        }, false),
      },
    });
    return this.bottomRotateCircle;
  }

  addEnt() {
    const cylinderPrimitive = this.addCone();
    const bottomCircle = this.addBottomCircle();
    const bottomRotateCircle = this.addBottomRotateCircle();
    return {
      cylinderPrimitive,
      bottomCircle,
      bottomRotateCircle,
    };
  }

  //移除光柱体
  remove() {
    this.viewer.entities.remove(this.bottomRotateCircle);
    this.viewer.entities.remove(this.bottomCircle);
    this.viewer.scene.primitives.remove(this.cylinderPrimitive);
  }
}
